package com.shanling.shanlingcontroller.utils;

import android.content.Context;
import android.widget.Toast;

/**
 * Created by Admin on 2017/9/28.
 * 提示词工具类
 */
public class ToastUtils {

    public static Toast mToast = null;


    /**
     * 弹出longToast
     * @param context 上下文
     * @param resId    资源Id
     */
    public static void showLongToast(Context context, int resId) {
        if (mToast == null) {
            mToast = Toast.makeText(context.getApplicationContext(), context.getString(resId), Toast.LENGTH_LONG);
        } else {
            mToast.setText(context.getString(resId));
            mToast.setDuration(Toast.LENGTH_LONG);
        }

        mToast.show();
    }


    /**
     * 弹出短Toast
     * @param context 上下文
     * @param resId    资源Id
     */
    public static void showToast(Context context, int resId) {
        if (mToast == null) {
            mToast = Toast.makeText(context.getApplicationContext(), context.getString(resId), Toast.LENGTH_SHORT);
        } else {
            mToast.setText(context.getString(resId));
            mToast.setDuration(Toast.LENGTH_SHORT);
        }

        mToast.show();
    }

    /**
     * 弹出短Toast
     * @param context 上下文
     * @param text
     */
    public static void showToast(Context context, String text) {
        if (mToast == null) {
            mToast = Toast.makeText(context.getApplicationContext(), text, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(text);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }

        mToast.show();
    }

    /**
     * 弹出Toast
     * @param context 上下文对象
     * @param text    提示的文本
     * @param duration 持续时间（0：短；1：长）
     * @param gravity  位置（Gravity.CENTER;Gravity.TOP;...）
     */
    public static void showToast(Context context, String text, int duration, int gravity) {
        if (mToast == null) {
            mToast = Toast.makeText(context.getApplicationContext(), text, duration);
        } else {
            mToast.setText(text);
            mToast.setDuration(duration);
        }
        mToast.setGravity(gravity, 0, 0);
        mToast.show();
    }

    /**
     * 弹出Toast
     * @param context 上下文对象
     * @param resId    文本资源ID
     * @param duration 持续时间（0：短；1：长）
     * @param gravity  位置（Gravity.CENTER;Gravity.TOP;...）
     */
    public static void showToast(Context context, int resId, int duration, int gravity) {
        if (mToast == null) {
            mToast = Toast.makeText(context.getApplicationContext(), context.getString(resId), duration);
        } else {
            mToast.setText(context.getString(resId));
            mToast.setDuration(duration);
        }
        mToast.setGravity(gravity, 0, 0);
        mToast.show();
    }

    /**
     * 关闭Toast
     */
    public static void cancelToast(){
        if(mToast !=null){
            mToast.cancel();
        }
    }
}
